package com.framework.Module.flow.flowcourse;

import com.framerock.baselib.Jiami;
import com.framerock.baselib.Json;
import com.framerock.baselib.Rock;
import com.framerock.baselib.RockList;
import com.framework.ACore.Model;
import com.framework.Flow.Flow;
import com.framework.Flow.FlowListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class FlowListenerflowcourse extends FlowListener {

    //有模块js
    public Boolean onModejs() { return true;}


    public Map<String, String> onDataafter()
    {
        Map<String, String> da = Rock.getMap();
        if(mRequest.LOADCI==1 && mRequest.COMPANYBOOL){

            da.put("modearr", Json.getListString(
                    Model.get(this.nDb, "flowunit").getAll("`cid`="+COMPANYID+" AND `isflow`>0", "id,name,type", "sort")
            ));
        }

        return da;
    }

    public Map<String, String> onDatabefore(){
        String where  = "";
        int fmodeid = Integer.parseInt(mRequest.get("fmodeid", "0"));
        if(fmodeid>0)where  = "`fmodeid`="+fmodeid+"";
        return Rock.getMap(new String[]{
                "where", where
        });
    }

    @Override
    public void onSaveafter(Map<String, String> da, String mid, Boolean isadd) {
        pipei(da.get("fmodeid"));
    }

    public Map<String, String> onReplacers(Map<String, String> da, int lx) {
        String untid = da.get("fmodeid");
        Map<String, String> urs;
        if(!Rock.isEmpt(untid)){
            urs = mFlow.mFlowBase.Unit().unitModel.getXinxi(untid);
            if(urs != null){
                da.put("fmodeid_textcn", ""+untid+"."+urs.get("name")+"");
            }
        }
        if(lx==2 && !Rock.isEmpt(da.get("where"))){
            da.put("where_textcn", "有条件");
            da.put("where_color", "gray");
           // da.put("where_title", Jiami.base64decode(da.get("where")));
        }
        return da;
    }

    @Override
    public String onAction(String act, String params) {
        if(act.equals("pipei"))return pipei("");
        return "";
    }

    /**
     * 匹配流程
     * */
    private String pipei(String fmodeid)
    {
        String where = "`cid`="+COMPANYID+" AND `status` NOT IN(1,5)";
        if(fmodeid.length() > 0)where+=" AND `fmodeid`="+fmodeid+"";
        ArrayList<Map<String, String>> rows = mFlow.mFlowBase.Bill().billModel.getAll(where);
        int i,len = rows.size(),yc=0,zt;Map<String, String> rs,da;Flow flow;String num;RockList rocklist;
        Map<String, Flow> flowMap = new HashMap<>();
        for(i=0;i<len;i++){
            rs      = rows.get(i);num = rs.get("fmodenum");
            zt      = Rock.getMapint(rs, "status");
            flow    = flowMap.get(num);
            if(flow == null){
                flow = Flow.get(this.nDb, num, mRequest);
                flowMap.put(num, flow);
            }
            flow.initFunit(rs.get("fmodeid"));
            flow.initData(rs.get("mid"));
            rocklist = flow.getFlowCourse();//匹配
            da = rocklist.getMap();
            if (zt != 2 && Rock.isEmpt(da.get("nowchecknameid"))) yc++;
        }
        flowMap = null;
        return "共匹配流程"+len+"个，异常"+yc+"个";
    }
}
